﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class JLTAB55
    {
        #region-------------Properties-----------------------
        #region ==WPATTERN===========
        public static readonly DependencyProperty WPATTERNProperty = DependencyProperty.Register("WPATTERN",
            typeof(string), typeof(JLTAB55),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWPATTERNChanged));
        public string WPATTERN
        {
            get
            {
                return (string)GetValue(WPATTERNProperty);
            }
            set
            {
                SetValue(WPATTERNProperty, value);
            }
        }
        private static void OnWPATTERNChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as JLTAB55;
            if (src != null)
            {
                src.WPATTERN = (string)e.NewValue;
            }
        }
        #endregion
        #region ==FPATTERN===========
        public static readonly DependencyProperty FPATTERNProperty = DependencyProperty.Register("FPATTERN",
            typeof(string), typeof(JLTAB55),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFPATTERNChanged));
        public string FPATTERN
        {
            get
            {
                return (string)GetValue(FPATTERNProperty);
            }
            set
            {
                SetValue(FPATTERNProperty, value);
            }
        }
        private static void OnFPATTERNChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as JLTAB55;
            if (src != null)
            {
                src.FPATTERN = (string)e.NewValue;
            }
        }
        #endregion

        #region ==ARTICLE===========
        public static readonly DependencyProperty ARTICLEProperty = DependencyProperty.Register("ARTICLE",
            typeof(string), typeof(JLTAB55),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnARTICLEChanged));
        public string ARTICLE
        {
            get
            {
                return (string)GetValue(ARTICLEProperty);
            }
            set
            {
                SetValue(ARTICLEProperty, value);
            }
        }
        private static void OnARTICLEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as JLTAB55;
            if (src != null)
            {
                src.ARTICLE = (string)e.NewValue;
            }
        }
        #endregion

        JLTAB55Data m_Data,m_DataX;
        ITXEXRPT ITXEXRPT;
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        } 
 
        #endregion
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        public DataSet dsParamaters;
        private DataRow dwUI;
        public DataRow dwGASTGA;
        DataTable tableUI;
        public string fromdate = "", enddate = "";
        public string KRS = "", LFD = "",POS = "";
        public JLTAB55()
        {
            InitializeComponent();
            this.Name = "织造上机单";
            m_Data = new JLTAB55Data();
            ITXEXRPT = new ITXEXRPT();
            ITXEXRPT.Root = "C:\\IntexReport";
            ITXEXRPT.PrintType = "OpenExcel";   

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            base.SetUp(dicParamater);
            InitializeComponent();
            m_Data = new JLTAB55Data();
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("KRS", typeof(string));
            tableUI.Columns.Add("LFD", typeof(string));
            tableUI.Columns.Add("POS", typeof(string));

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow();
                txtLFD.Focus();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    KRS = txtKRS.Text.ToString().Trim();
                    LFD = m_Data.Right("000000" + txtLFD.Text.ToString().Trim(), 6);
                    POS = m_Data.Right("000" + txtPOS.Text.ToString().Trim(), 3);
                    
                    dwUI["KRS"] = KRS;
                    dwUI["LFD"] = LFD;
                    dwUI["POS"] = POS;
                    setResult = m_Data.BuildData(dwUI);
                    ARTICLE = "";
                    WPATTERN = "";
                    FPATTERN = "";
                    //cWTF.INFO = null;
                    foreach (DataRow row in setResult.Tables[0].Rows)
                    {
                        
                        ARTICLE = row["ABPART"].ToString()+ row["ABPVAR"].ToString().Substring(0,2).PadRight(3)+ row["ABPFARBE"].ToString();
                        WPATTERN =("J"+ row["ARTICLE"].ToString().Trim()).PadRight(15);
                        FPATTERN = ("W" + row["ARTICLE"].ToString().Trim()).PadRight(15);
                        int i = 0;
                        foreach(DataRow ws33 in cWS33.TBMain.Rows)
                        {
                            string des = Helpers.Helper.GetGB2312Value(ws33["WAGBEM"].ToString().Trim());
                            
                            cWTF.INFO = cWTF.INFO.Replace(cWTF.TMV.WA[i], cWTF.TMV.WA[i] + des);
                            //if (cWTF.INFO=="花型未导入")
                            //{
                            //    cWTF.INFO = "A" + des + "1";
                            //}
                            i++;
                        }
                        //if (i == 0)
                        //{
                        //    row["WPATTERN"] = "A1";
                        //    row["FPATTERN"] = "A1";
                        //}
                        //else
                        //{
                        //    row["WPATTERN"] = cWTW.INFO;
                        //    row["FPATTERN"] = cWTF.INFO;
                        //}
                        //if (cWTF.INFO=="花型未导入")
                        //{
                        //    row["WPATTERN"] = "A1";
                        //          row["FPATTERN"]="A1";
                        //}
                        row["WPATTERN"] = cWTW.INFO;
                           row["FPATTERN"] = cWTF.INFO;
                           if (cWTF.INFO == "花型未导入")
                           {
                               row["WPATTERN"] = "A1";
                               row["FPATTERN"] = "A1";
                           }
                    }                   
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["AB55"];
                    dlDg.DataContext = setUI.Tables["WB"];

                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            
            try
            {                
                m_DataX = new JLTAB55Data(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = INTEXDBC.OracleDB;
                m_DataX.proxy = INTEXDBC.proxy;
                JLTAB55Report m_rpt = new JLTAB55Report(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.PrintType = "OpenExcel";
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.KRS = KRS;
                m_rpt.LFD = LFD;
                m_rpt.POS = POS;
                m_rpt.WriteExcelReport("OpenExcel");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void btnBARCODE_Click(object sender, RoutedEventArgs e)
        {

            try
            { 
                DataSet Set = new DataSet();
                Set.Merge(setUI); 
                cDR.PRINT(Set);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnQuery_Click(null, null);
            }
        }

        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }


    }
}